package com.xmy.aboutreflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

// 反射相关
public class Demo1 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        // 使用 Class.forName() 获取Class对象会初始化对象
        Class<?> targetClass = Class.forName("com.xmy.aboutreflect.TargetObject");

        TargetObject targetObject = (TargetObject) targetClass.newInstance();

        Method pMethod = targetClass.getDeclaredMethod("publicMethod", String.class, int.class);
        pMethod.invoke(targetObject, "Hello", 99);

        Method priMethod = targetClass.getDeclaredMethod("privateMethod");
        priMethod.setAccessible(true);
        priMethod.invoke(targetObject);

        Field field = targetClass.getDeclaredField("value");
        field.setAccessible(true);
        field.set(targetObject, "TargetClass");

        priMethod.invoke(targetObject);

    }
}
